import ZIM from '@/js_sdk/zego-ZIMUniplugin-JS/lib/ZIM.js';
import { httpTokenRequest } from '@/utils/http.js';
import { getUserInfo } from '@/api/my.js'
export default {
	data() {
		var checkMobile = (rule, value, callback) => {
			if (!value) {
				return callback(new Error('请输入手机号'));
			}
			var reg = /^1[3456789]\d{9}$/;
			if (!reg.test(value)) {
				return callback(new Error('请输入正确的手机号'));
			} else {
				return callback();
			}
		};
		return {
			rules: {
				account: [
					{
						validator: checkMobile,
						trigger: 'blur'
					}
				],
				password: {
					required: true,
					message: '请输入密码',
					trigger: ['blur']
				},
			},
			form: {
				account: '', //	是	string	手机号
				password: "", //		是	string	密码
				app_version: "", //版本号
			},
			checked: false,
			isShow: false,
		};
	},
	methods: {
		isConfirm() {
			this.checked = true;
			this.isShow = false;
			this.signInNow()
		},
		//回到上一页
		goBack() {
			let page = getCurrentPages();
			if (page.length == 1) {
				uni.switchTab({
					url: '/pages/index/index'
				});
			} else {
				uni.navigateBack({
					delta: 1
				});
			}
		},
		//checkbox
		onCheck(e) {
			this.checked = !this.checked;
		},

		//登录
		signInNow() {
			if (this.form.account == "") {
				this.$tools.showToast("请输入手机号");
				return false;
			}
			if (this.form.password == "") {
				this.$tools.showToast("请输入密码");
				return false;
			}
			if (this.checked == false) {
				this.isShow = true;
				console.log(this.isShow, '11111111111111111');
				// this.$tools.showToast("请阅读并同意以下协议");
				return false;
			}

			var data = this.form;
			data.number = uni.getDeviceInfo().deviceModel
			data.cid = uni.getStorageSync('cid')
			data.app_version = uni.getStorageSync('version')
			httpTokenRequest({
				methods: 'POST',
				url: this.$paths.login,
				params: data
			})
				.then(res => {
					if (res.code == 1) {
						this.$tools.showToast("登录成功");
						uni.setStorageSync("token", res.data.token);
						uni.removeStorageSync('lastOpenTime')
						// 获取个人信息  存到本地
						getUserInfo().then(res => {
							console.log(res, '个人信息');
							uni.setStorageSync('userInfo', res.data)
						})
						uni.switchTab({
							url: "../home/index"
						})
						// #ifdef APP-PLUS
						this.$zegoZim.zegoLogin();
						// #endif
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch(err => {
					console.log('错误回调', err);
				});
		},
		jump(url) {
			uni.navigateTo({
				url: url
			})
		},
	},
	onLoad(e) {
		console.log(e, 'onLoad');
		if (e.tel) {
			this.form.account = e.tel;
		}
	},
	onLaunch() {

	},
	onShow() {

	},
};